package ipdesk.model.report.design.dao;

import ipdesk.model.report.brand.bean.BrandInfoBean;
import ipdesk.model.report.design.bean.DesignInfoBean;
import ipdesk.model.report.promote.bean.PromoteInfoBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;

import ecg.util.SessionUtils;

public class DesignInfoImpl extends SqlMapDaoTemplate implements IDesignInfo
{
	public DesignInfoImpl(DaoManager manager)
	{
		super(manager);
	}
	
	public int designInfoReg(DesignInfoBean designInfoBean) throws Exception
	{
		int designCodeCount = 0;
		int lastDesignSeq = 0;
		String designCode = "";
		
			
		String consultingTime = designInfoBean.getConsultingDate();
		String deskCode = designInfoBean.getDeskCode();
		
		// 기업코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		designCodeCount = ((Integer)this.queryForObject("design.designCode-count", designInfoBean)).intValue();
				
		String endNumber = StringUtils.leftPad(Integer.toString(designCodeCount+1), 4, "0");
		
		// 생성된 상표출원일지 관리코드.
		designCode = consultingTime.replaceAll("-","") + "-" + deskCode.substring(4) + "-DES-" + endNumber;
		
		
		// 생성된 기업관리코드 빈 객체에 저장.
		designInfoBean.setManageNo(designCode);
		designInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// iBatis 연동. 데이터 insert.
		lastDesignSeq = ((Integer)this.insert("design.designInfoInsert", designInfoBean)).intValue();
		
		return lastDesignSeq;
	}
	
	public void insertFileData(DesignInfoBean designInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("design.designFileInfoInsert", designInfoBean);
	}
	
	public int getDataTotalCount(DesignInfoBean designInfoBean) throws Exception
	{
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		
		/*if(designInfoBean.getDeskCode() == null || "".equals(designInfoBean.getDeskCode()))
		{
			map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		}else{*/
			map.put("deskCode", designInfoBean.getDeskCode());
		//}
		
		if(designInfoBean.getDesignSearchKeyword() != null || !"".equals(designInfoBean.getDesignSearchKeyword())){
			map.put("designSearchKeyword", designInfoBean.getDesignSearchKeyword());
			map.put("designSearchOption", designInfoBean.getDesignSearchOption());
		}
		
		if(designInfoBean.getYear() != null || !"".equals(designInfoBean.getYear())) {
			map.put("year", designInfoBean.getYear());
		}
		
		if(designInfoBean.getState() != null || !"".equals(designInfoBean.getState())) {
			map.put("state", designInfoBean.getState());
		}
		
		int totalDataCount = ((Integer)this.queryForObject("design.designInfoTotalcount", map)).intValue();

		return totalDataCount;
	}
	
	public List<DesignInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		System.out.println(map.get("startRow"));
		System.out.println(map.get("limitRowCount"));
		List<DesignInfoBean> pageDataList = this.queryForList("design.designPageDataListForUser", map);
		
		return pageDataList;
	}
	
	public DesignInfoBean getDetailData(int designSeq)
	{
		DesignInfoBean designInfoBean = (DesignInfoBean)this.queryForObject("design.designDetailData", designSeq);
		return designInfoBean;
	}
	
	/**
	 *  해당 designSeq의 하나의 로우 데이터를 가지고온다.
	 *  
	 */
	public DesignInfoBean getDataOneRow(int designSeq, String queryId)
	{
		DesignInfoBean designInfoBean = (DesignInfoBean)this.queryForObject(queryId, designSeq);
		
		return designInfoBean;
	}
	
	/**
	 * 해당 designSeq의 하나의 로우를 삭제한다.
	 */
	public void deleteDataOneRow(int designSeq,  String queryId)
	{
		this.delete(queryId, designSeq);
	}
	
	/**
	 * 해당 designSeq의 하나의 로우를 업데이트 한다.
	 * @param designInfoBean
	 * @param queryId
	 * @return
	 */
	public int designInfoUpdate(DesignInfoBean designInfoBean, String queryId)
	{
		int result = this.update(queryId, designInfoBean);
		
		return result;
	}

	/**
	 * 파일 상세보기
	 */
	public DesignInfoBean getFileDetailData(DesignInfoBean designInfoBean) {
		// TODO Auto-generated method stub
		return (DesignInfoBean)this.queryForObject("design.designUploadData", designInfoBean);
	}

	/**
	 * 완료보고서 등록
	 */
	public void insertCompleteData(DesignInfoBean designInfoBean) {
		// TODO Auto-generated method stub
		this.update("design.designCompleteInsert", designInfoBean);
	}

	/**
	 * 파일 삭제
	 */
	public void delFileData(String fileSeq) {
		// TODO Auto-generated method stub
		this.delete("design.deleteUploadData", fileSeq);
	}

}
